//
//  CartoonPlay.m
//  Double
//
//  Created by 袁庆秋 on 15/5/26.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CartoonPlay.h"

@interface CartoonPlay ()<UIWebViewDelegate>

@end

@implementation CartoonPlay

- (void)dealloc
{
    [_url release];
    [super dealloc];
}


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"url = %@", self.url);
    
    
// 蓝欧网络太渣， 不是代码问题。。。
    NSURL *url = [NSURL URLWithString:self.url];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height - 40)];
    webView.backgroundColor = [UIColor orangeColor];
    [webView setDelegate:self];
    [webView loadRequest:req];
    [self.view addSubview:webView];
    [webView release];
    
    
    //设置返回按钮
    UIButton *left = [UIButton buttonWithType:UIButtonTypeSystem];
    left.frame = CGRectMake(0, 0, 32, 32);
    [left setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [left addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:left];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)backAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    NSString *requestString = [[request URL] absoluteString];//获取请求的绝对路径.
    
    
    NSLog(@"url -======== %@", requestString);
    
    NSArray *components = [requestString componentsSeparatedByString:@":"];//提交请求时候分割参数的分隔符
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"auto con_player con_player_list"]) {
        //过滤请求是否是我们需要的.不需要的请求不进入条件
        if([(NSString *)[components objectAtIndex:1] isEqualToString:@"auto con_player con_player_list"])
        {
            NSString *message = (NSString *)[components objectAtIndex:2];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"JS向APP提交数据" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
        }
        return NO;
    }
    return YES;
}

@end
